package com.example.questionnairespringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnairespringboot.entity.User;
import com.example.questionnairespringboot.mapper.UserMapper;
import com.example.questionnairespringboot.service.UserService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

@EnableOpenApi
@RestController
@RequestMapping("/user")
public class UserController {
/*    @Autowired
    private UserMapper userMapper;*/
    @Autowired
    private UserService userService;

    @PostMapping
    public boolean save(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    @GetMapping
    public List<User> findAllUser(){
        return userService.list();
    }
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return userService.removeById(id);
    }
    @PostMapping("/del/batch")
    public boolean delete(@RequestBody List<Integer> ids){
        return userService.removeBatchByIds(ids);
    }
/*    @GetMapping("/page")
    public Map<String,Object> findPage(@RequestParam Integer pageNum,
        @RequestParam Integer pageSize,@RequestParam String username){
        Map<String,Object> res = new HashMap<>();
        res.put("data",userMapper.selectPage((pageNum-1)*pageSize,pageSize,username));
        res.put("total",userMapper.selectTotal(username));
        return res;
    }*/

    @GetMapping("/page")
    public IPage<User> findPage(@RequestParam Integer pageNum,
        @RequestParam Integer pageSize,@RequestParam(defaultValue = "") String username) {
        IPage<User> page = new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(!"".equals(username)){
            queryWrapper.like("username",username);
        }
        return userService.page(page,queryWrapper);
    }
}





















